package top.yamhk.agent.utils;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author 庆 HongKong
 */
@Slf4j
public class Dates {

    static final String NUMBER_DATE_FORMAT = "yyyyMMdd";
    static final String NUMBER_DATE_TIME_FORMAT = "yyyyMMddHHmmss";
    static final String DATE_PATTERN = "yyyy-MM-dd";
    static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    static final String MONTH_PATTERN = "yyyyMM";

    /**
     * 当前时间
     *
     * @return Date
     */
    public static Date now() {
        //return new Date(Calendar.getInstance().getTimeInMillis());
        return new Date();
    }

    /**
     * 当天时间 无时分秒
     *
     * @return Date
     */
    public static Date today() {
        return Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 转换某天带时分秒日期为年月日
     *
     * @return String
     */
    public static String formatToday() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(NUMBER_DATE_FORMAT);
        return formatter.format(LocalDate.now());
    }

    /**
     * 转换某天带时分秒日期为年月日
     *
     * @return String
     */
    public static String formatTodayTime() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
        return formatter.format(LocalDateTime.now());
    }

    /**
     * 转换某天带时分秒日期为年月日
     *
     * @return String
     */
    public static String formatTodayTimeNumber() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(NUMBER_DATE_TIME_FORMAT);
        return formatter.format(LocalDateTime.now());
    }

    /**
     * 将日期转换成指定格式字符串
     *
     * @param date   date
     * @param format format
     * @return String
     */
    public static String format(Date date, String format) {
        if (date == null) {
            return "";
        }
        return DateFormatUtils.format(date, format);
    }

    /**
     * 将日期转换成指定格式字符串 yyyy-MM-dd
     *
     * @param date date
     * @return String
     */
    public static String formatDateWithHeng(Date date) {
        return DateFormatUtils.format(date, DATE_PATTERN);
    }

    /**
     * 将日期转换成指定格式字符串 yyyyMMdd
     *
     * @param date date
     * @return String
     */
    public static String formatDate(Date date) {
        return DateFormatUtils.format(date, NUMBER_DATE_FORMAT);
    }

    /**
     * 将日期转换成指定格式字符串 yyyy-MM-dd HH:mm:ss
     *
     * @param date date
     * @return String
     */
    public static String formatDateTime(Date date) {
        return DateFormatUtils.format(date, DATE_TIME_PATTERN);
    }


    /**
     * 转换某天带时分秒日期为年月日
     *
     * @param dateTimeStr dateTimeStr
     * @return String
     */
    public static String formatThatDay(String dateTimeStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
        return formatter.format((TemporalAccessor) Dates.parseDateTime(dateTimeStr));
    }

    /**
     * 日期字符串转换成日期
     *
     * @param dateStr dateStr
     * @return Date
     */
    public static Date parseDate(String dateStr) {
        return parse(dateStr, DATE_PATTERN);
    }

    /**
     * 带时分秒日期转换成日期
     *
     * @param dateTimeStr dateTimeStr
     * @return Date
     */
    public static Date parseDateTime(String dateTimeStr) {
        return parse(dateTimeStr, DATE_TIME_PATTERN);
    }

    /**
     * 带时分秒日期转换成日期
     *
     * @param dateTimeStr dateTimeStr
     * @return Date
     */
    public static Date parseNumberDateTime(String dateTimeStr) {
        return parse(dateTimeStr, NUMBER_DATE_TIME_FORMAT);
    }

    /**
     * 解析字符串日期
     *
     * @param dateStr dateStr
     * @param pattern pattern
     * @return Date
     */
    @SneakyThrows
    private static Date parse(String dateStr, String pattern) {
        return DateUtils.parseDateStrictly(dateStr, pattern);
    }

    /**
     * 相隔多少天
     *
     * @param startTime startTime
     * @param endTime   endTime
     * @return long
     */
    public static long dayBetween(Date startTime, Date endTime) {
        long diff = (endTime.getTime() - startTime.getTime()) / 86400 / 1000;
        return Math.abs(diff);
    }

    /**
     * 获取当前小时-时
     *
     * @return long
     */
    public static Integer getHour() {
        Calendar calendar = getCalendar();
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取当前小时-分
     *
     * @return long
     */
    public static Integer getMinute() {
        Calendar calendar = getCalendar();
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取当前小时-秒
     *
     * @return long
     */
    public static Integer getSecond() {
        Calendar calendar = getCalendar();
        return calendar.get(Calendar.SECOND);
    }

    private static Calendar getCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar;
    }
}
